﻿package com.bjypc.youth.web.excel;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.bjypc.youth.entity.AdultPerson;
import com.bjypc.youth.entity.FutureJudgment;
import com.bjypc.youth.entity.ImportentLevel;
import com.bjypc.youth.entity.InterpersonalRelationship;
import com.bjypc.youth.entity.LifeStyle;
import com.bjypc.youth.entity.PersonConsume;
import com.bjypc.youth.entity.PersonalFeeling;
import com.bjypc.youth.entity.PersonalInterest;
import com.bjypc.youth.entity.TimeUsing;
import com.bjypc.youth.service.IAdultPersonService;
import com.bjypc.youth.util.BjyouthConstants;
import com.opensymphony.xwork2.ActionSupport;

public class ExportExcelAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private IAdultPersonService adultPersonService;

	public void prepare() throws Exception {
	}

	/**
	 * 绘出Excel
	 * 
	 * @param actionMapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		List<AdultPerson> dataList = adultPersonService.findAll();
		HSSFWorkbook workbook = exportExcel(dataList);
		if (workbook != null) {
			this.printExcel(workbook, response, "AdultData");
		}
		return SUCCESS;
	}

	// 导出Excel

	private void printExcel(HSSFWorkbook workbook,
			HttpServletResponse response, String fileName) throws IOException {
		OutputStream out = response.getOutputStream();
		response.setHeader("Content-disposition", "attachment; filename="
				+ fileName + ".xls");
		response.setContentType("application/msexcel;charset=UTF-8");
		workbook.write(out);
		out.flush();
		out.close();

	}

	public HSSFWorkbook exportExcel(List<AdultPerson> dataList)
			throws Exception {
		HSSFWorkbook workbook = this.adultPersonService.export(dataList);
		return workbook;

	}

	public IAdultPersonService getAdultPersonService() {
		return adultPersonService;
	}

	public void setAdultPersonService(IAdultPersonService adultPersonService) {
		this.adultPersonService = adultPersonService;
	}

}
